package com.chuanye.mydrawable.adapters;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;


import com.chuanye.mydrawable.R;
import com.chuanye.mydrawable.beans.City;

import java.util.List;

/**
 * Created by lsef on 2019/1/15.
 */

public class SpinnerOneAdapter extends BaseAdapter{
    private Context mContext;
    private List<City.CityBean> cityBeanList;

    public SpinnerOneAdapter(Context mContext, List<City.CityBean> cityBeanList) {
        this.mContext = mContext;
        this.cityBeanList = cityBeanList;
    }

    @Override
    public int getCount() {
        return cityBeanList.size();
    }

    @Override
    public Object getItem(int i) {
        return cityBeanList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder holder = null;
        if (view == null){
            view = View.inflate(mContext, R.layout.spinner_one_item,null);
            holder = new ViewHolder();
            holder.name = view.findViewById(R.id.spinner_one_name);
            holder.lv = view.findViewById(R.id.lv);

            view.setTag(holder);
        }
        holder = (ViewHolder) view.getTag();
        holder.name.setText(cityBeanList.get(i).getCityname());

        final List<City.CityBean.CountyBean> countyBeanList = cityBeanList.get(i).getCounty();
        SpinnerTwoAdapter adapter = new SpinnerTwoAdapter(mContext,countyBeanList);
        holder.lv.setAdapter(adapter);
        holder.lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Toast.makeText(mContext,""+countyBeanList.get(i).getName(),Toast.LENGTH_SHORT).show();

               if (subClickListener != null){
                   subClickListener.OntopicClickListener(view,countyBeanList.get(i),i);
               }
            }
        });

        /*使用这个方法来获取listview的高度，需要注意一下几个问题：
        1、listview的item的根布局一定要是LinearLayout；
        2、调用这个方法需要在适配器数据加载更新之后；代码如下：*/
        adapter.notifyDataSetChanged();
        setListViewHeightBasedOnChildren(holder.lv);





        return view;
    }

    private static class ViewHolder{
        TextView name;
        ListView lv;
    }

    public static void setListViewHeightBasedOnChildren(ListView listView) {
        ListAdapter listAdapter = listView.getAdapter(); if (listAdapter == null) { // pre-condition
            return;
        }
        int totalHeight = 0; for (int i = 0; i < listAdapter.getCount(); i++) {
            View listItem = listAdapter.getView(i, null, listView); // listItem.measure(0, 0);
            listItem.measure(
                    View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
                    View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
            totalHeight += listItem.getMeasuredHeight();
        }

        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
        listView.setLayoutParams(params);
    }

//---------------------

    private SubClickListener subClickListener;

    public void setSubClickListener(SubClickListener topicClickListener){
        this.subClickListener = topicClickListener;

    }

    public interface SubClickListener{
        void OntopicClickListener(View view, City.CityBean.CountyBean been, int position);
    }




}
